package ui.xmlVisualization;

import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;

import java.awt.Paint;
import java.awt.Shape;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;

/**
 * 
 * @author huangcd
 * 
 */
public class XMLVertex extends DirectedSparseVertex
{

    protected int   level;
    protected Paint fillPaint;
    protected Paint drawPaint;
    protected Shape shape;
    protected Node  source;

    public XMLVertex(Node source)
    {
        this.source = source;
    }

    public int getLevel()
    {
        return level;
    }

    public Paint getFillPaint()
    {
        return fillPaint;
    }

    public Paint getDrawPaint()
    {
        return drawPaint;
    }

    public Shape getShape()
    {
        return shape;
    }

    public void setLevel(int level)
    {
        this.level = level;
    }

    public void setFillPaint(Paint fillPaint)
    {
        this.fillPaint = fillPaint;
    }

    public void setDrawPaint(Paint drawPaint)
    {
        this.drawPaint = drawPaint;
    }

    public void setShape(Shape shape)
    {
        this.shape = shape;
    }

    @SuppressWarnings("unchecked")
    @Override
    public String toString()
    {
        String content = "";
        if (source instanceof Element)
        {
            StringBuilder buf = new StringBuilder(source.getName());
            int attributeCount = ((Element) source).attributeCount();
            if (attributeCount != 0)
            {
                buf.append(" [");
                List<Attribute> list = ((Element) source).attributes();
                for (Attribute attr : list)
                {
                    buf.append(attr.getName()).append(":").append(
                            attr.getValue()).append(";");
                }
                buf.deleteCharAt(buf.length() - 1);
                buf.append("]");
            }
            content = buf.toString();
        }
        else if (source instanceof Text)
        {
            content = " [" + ((Text) source).getText() + "]";
        }
        else
        {
            content = " [" + source.asXML() + "]";
        }
        return getLevel() + ":" + content;
    }

    /**
     * return the content of this XMLVertex with or without attributes
     * 
     * @param showAttributes
     * @return
     */
    public String toString(boolean showAttributes)
    {
        if (showAttributes)
            return toString();
        else
        {
            String content = "";
            if (source instanceof Element)
            {
                content = source.getName();
            }
            else if (source instanceof Text)
            {
                content = " [" + ((Text) source).getText() + "]";
            }
            return getLevel() + ":" + content;
        }
    }
}
